<?php
	session_start();
	if(!isset($_SESSION["jara_admin"])) {
		header("Location: ../login.php");
		exit;
	}
	if($_SESSION["jara_permissions"][2] == 0) {	
		header("Location: error.php");
		exit;
	}
	require_once("../include/templating_fns.php");
	require_once("../include/db_fns.php");
	jara_page_start("Edit Category - Administrator CP");
	$id = $_REQUEST["id"];
	if(isset($_REQUEST["edit_action"])) {
		try {
			$title = $_POST["title"];
			$name = strtolower(preg_replace("/[^a-zA-Z0-9]/", "-", $_POST["title"]));
			if(!get_magic_quotes_gpc()) {
				$title = addslashes($title);
				$name = addslashes($name);
			}
			if($id == 1) {
				throw new JaraGeneralException("You cannot edit the Uncategorized category.");
			}
			$query = "update jara_categories set title = '$title', name = '$name' where categoryid = '$id'";
			$result = jara_db_query($query);
			if($result == 0) {
				throw new JaraGeneralException("No information has been changed in the database.");
			}
			else {
				echo "<p>Edited category successfully.<br /><a href=\"categories.php\">Return to Categories</a></p>";
				jara_page_end();
				exit;
			}
		}
		catch(JaraDatabaseException $ex) {
?>
	<p>
		A database error occured during the process of creating this page.
	</p>
	<p>
		Error type: <?php echo (($ex->getSqlStage() == 1) ? "MySQL connection error" : "MySQL query error"); ?><br />
		Error message: &quot;<?php echo $ex->getSqlMsg(); ?>&quot; [code <?php echo $ex->getSqlCode(); ?>]
	</p>
<?php
			jara_page_end();
			exit;
		}
		catch(JaraGeneralException $ex) {
?>
	<p>
		A general error occured during the process of creating this page.
	</p>
	<p>
		Error message: &quot;<?php echo $ex->getMessage(); ?>&quot;
	</p>
<?php
			jara_page_end();
			exit;
		}
		exit;
	}
	try {
		if($id == 1) {
?>
<p>
	Sorry, you cannot edit the Uncategorized category.<br />
	<a href="categories.php">Return to category listing</a>
</p>
<?php
			jara_page_end();
			exit;
		}
?>
<form action="edit_category.php" method="post">
<p>
<input type="hidden" name="edit_action" id="edit_action" value="change" />
<input type="hidden" name="id" id="id" value="<?php echo $id; ?>" />
<?php
	$result = jara_db_query("select * from jara_categories where categoryid = '$id'");
	if($result->num_rows == 0) {
		throw new JaraGeneralException("The requested category does not exist.");
	}
	$row = $result->fetch_assoc();
?>
<h3>Category Details</h3>
<p>
New title: <input type="text" name="title" id="title" value="<?php echo $row["title"]; ?>" /><br />
</p>
<p>
<input type="submit" value="Save" />
</p>
</form>
<?php
	}
	catch(JaraDatabaseException $ex) {
?>
<p>
A database error occured during the process of creating this page.
</p>
<p>
Error type: <?php echo (($ex->getSqlStage() == 1) ? "MySQL connection error" : "MySQL query error"); ?><br />
Error message: &quot;<?php echo $ex->getSqlMsg(); ?>&quot; [code <?php echo $ex->getSqlCode(); ?>]
</p>
<?php
		jara_page_end();
		exit;
	}
	catch(JaraGeneralException $ex) {
?>
<p>
A general error occured during the process of creating this page.
</p>
<p>
Error message: &quot;<?php echo $ex->getMessage(); ?>&quot;
</p>
<?php
		jara_page_end();
		exit;
	}
	jara_page_end();
?>
	